package com.simpleaccount;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.simpleaccount.Cloud.CloudOperations;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.Graph.GraphFragment;
import com.simpleaccount.Home.HomeFragment;
import com.simpleaccount.Login.LoginActivity;
import com.simpleaccount.User.UserFragment;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import es.dmoral.toasty.Toasty;


public class MainActivity extends FragmentActivity {

    @Bind(R.id.ivHome)
    ImageView ivHome;
    @Bind(R.id.ivGraph)
    ImageView ivGraph;
    @Bind(R.id.ivUser)
    ImageView ivUser;

    private FragmentManager fManager;
    private HomeFragment fmHome;
    private GraphFragment fmGraph;
    private UserFragment fmUser;

    float x1 = 0;
    float x2 = 0;
    float y1 = 0;
    float y2 = 0;
    int page=0;
    View v1;
//    ImageView ivGraph;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this,"3b0b94c540b227f4a462387bb42a4cc6");
        setContentView(R.layout.activity_main);
        DbUtils.init(this);
        ButterKnife.bind(this);
        initView();
        Toasty.Config.getInstance().setErrorColor(Color.parseColor("#ff0000")).apply();
        clickMenu(0);
    }
    @Override
    protected void onStart() {
        super.onStart();

    }
    @Override
    protected void onResume() {
        super.onResume();

    }


    private void initView() {
        fManager = getSupportFragmentManager();

    }
    public void clickMenu(int vID) {
        FragmentTransaction trans = fManager.beginTransaction();
        hideFragment(trans);
        setFragment(vID, trans);
        trans.commit();
        page=vID;
    }
    public void hideFragment(FragmentTransaction trans) {
        ivHome.setImageResource(R.drawable.homepage);
        ivGraph.setImageResource(R.drawable.chart);
        ivUser.setImageResource(R.drawable.usernew);
        if (null != fmHome) {
            trans.hide(fmHome);
        }
        if (null != fmGraph) {
            trans.hide(fmGraph);
        }
        if (null != fmUser) {
            trans.hide(fmUser);
        }
    }
    private void setFragment(int vID, FragmentTransaction trans) {
        switch (vID) {
            case 0:
                ivHome.setImageResource(R.drawable.homepage2);
                if (null == fmHome) {
                    fmHome = new HomeFragment();
                    trans.add(R.id.fm_content, fmHome);
                } else {
                    trans.show(fmHome);

                }
                break;
            case 1:
                ivGraph.setImageResource(R.drawable.chart2);
                if (null == fmGraph) {
                    fmGraph = new GraphFragment();
                    trans.add(R.id.fm_content, fmGraph);
                } else {
                    trans.show(fmGraph);

                }
                break;
            case 2:
                ivUser.setImageResource(R.drawable.usernew2);
                if (null == fmUser) {
                    fmUser = new UserFragment();
                    trans.add(R.id.fm_content, fmUser);
                } else {
                    trans.show(fmUser);
                }
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.ivHome, R.id.ivGraph, R.id.ivUser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivHome:
                clickMenu(0);
                break;
            case R.id.ivGraph:
                clickMenu(1);
                break;
            case R.id.ivUser:
                clickMenu(2);
                break;
        }
    }
    private void addTestingData() {

        List<Category> category=DbUtils.getCategory();
        DbUtils.getCount();
        String strTest="这是一段用于测试的文本";
        for(int i=0;i<21;i++){
            strTest=strTest+"测试的文本";
            Category category1=category.get(i%21);
            Entry e = new Entry(DbUtils.getCount(),Math.random()*(category1.getType()==0?-100:100),category1,randomDateBetweenMinAndMax(),strTest+i);
            DbUtils.insertEntry(e);
        }

    }

    public static Date randomDateBetweenMinAndMax(){
        Calendar calendar = Calendar.getInstance();
        //注意月份要减去1
        calendar.set(2017,5,1);
        calendar.getTime().getTime();
        //根据需求，这里要将时分秒设置为0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND,0);
        long min = calendar.getTime().getTime();
        calendar.set(2017,7,15);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.getTime().getTime();
        long max = calendar.getTime().getTime();
        //得到大于等于min小于max的double值
        double randomDate = Math.random()*(max-min)+min;
        //将double值舍入为整数，转化成long类型
        calendar.setTimeInMillis(Math.round(randomDate));
        return calendar.getTime();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //继承了Activity的onTouchEvent方法，直接监听点击事件
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //当手指按下的时候
            x1 = event.getX();
            y1 = event.getY();
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            //当手指离开的时候
            x2 = event.getX();
            y2 = event.getY();
            if(y1 - y2 > 50) {
                //Toast.makeText(MainActivity.this, "向上滑", Toast.LENGTH_SHORT).show();
            } else if(y2 - y1 > 50) {
                //Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show();
            } else if(x1 - x2 > 50) {
                //Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
                if(page==0||page==1) {
                    clickMenu(page + 1);
                }

            } else if(x2 - x1 > 50) {
                //Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
                if(page==1||page==2) {
                        clickMenu(page - 1);

                    }

            }
        }
        return super.onTouchEvent(event);
    }




}




